package com.shareit.ocr.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shareit.ocr.enums.ResultEnum;

/**
 * 
 * @author geai
 *
 */
public class InvoiceQueryUtil {

	private static final List<InvoiceCityEntity> citys = new ArrayList<InvoiceCityEntity>();
	private static final List<String> codes = new ArrayList<String>();
	private static final Map<String,String> comments = new HashMap<String, String>();
	private static final String URL_SUBFIX_YZM = "/WebQuery/yzmQuery";
	private static final String URL_SUBFIX_DETAIL = "/WebQuery/query";

	static {
		citys.add(new InvoiceCityEntity("1100", "北京",
				"https://zjfpcyweb.bjsat.gov.cn:443",
				"https://zjfpcyweb.bjsat.gov.cn:443"));
		citys.add(new InvoiceCityEntity("1200", "天津",
				"https://fpcy.tjsat.gov.cn:443",
				"https://fpcy.tjsat.gov.cn:443"));
		citys.add(new InvoiceCityEntity("1300", "河北",
				"https://fpcy.he-n-tax.gov.cn:82",
				"https://fpcy.he-n-tax.gov.cn:82"));
		citys.add(new InvoiceCityEntity("1400", "山西",
				"https://fpcy.sx-n-tax.gov.cn:443",
				"https://fpcy.sx-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("1500", "内蒙古",
				"https://fpcy.nm-n-tax.gov.cn:443",
				"https://fpcy.nm-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("2100", "辽宁",
				"https://fpcy.tax.ln.cn:443", "https://fpcy.tax.ln.cn:443"));
		citys.add(new InvoiceCityEntity("2102", "大连",
				"https://fpcy.dlntax.gov.cn:443",
				"https://fpcy.dlntax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("2200", "吉林",
				"https://fpcy.jl-n-tax.gov.cn:4432",
				"https://fpcy.jl-n-tax.gov.cn:4432"));
		citys.add(new InvoiceCityEntity("2300", "黑龙江",
				"https://fpcy.hl-n-tax.gov.cn:443",
				"https://fpcy.hl-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("3100", "上海",
				"https://fpcyweb.tax.sh.gov.cn:1001",
				"https://fpcyweb.tax.sh.gov.cn:1001"));
		citys.add(new InvoiceCityEntity("3200", "江苏",
				"https://fpdk.jsgs.gov.cn:80", "https://fpdk.jsgs.gov.cn:80"));
		citys.add(new InvoiceCityEntity("3300", "浙江",
				"https://fpcyweb.zjtax.gov.cn:443",
				"https://fpcyweb.zjtax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("3302", "宁波",
				"https://fpcy.nb-n-tax.gov.cn:443",
				"https://fpcy.nb-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("3400", "安徽",
				"https://fpcy.ah-n-tax.gov.cn:443",
				"https://fpcy.ah-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("3500", "福建",
				"https://fpcyweb.fj-n-tax.gov.cn:443",
				"https://fpcyweb.fj-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("3502", "厦门",
				"https://fpcy.xm-n-tax.gov.cn",
				"https://fpcy.xm-n-tax.gov.cn"));
		citys.add(new InvoiceCityEntity("3600", "江西",
				"https://fpcy.jxgs.gov.cn:82", "https://fpcy.jxgs.gov.cn:82"));
		citys.add(new InvoiceCityEntity("3700", "山东",
				"https://fpcy.sd-n-tax.gov.cn:443",
				"https://fpcy.sd-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("3702", "青岛",
				"https://fpcy.qd-n-tax.gov.cn:443",
				"https://fpcy.qd-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("4100", "河南",
				"https://fpcy.ha-n-tax.gov.cn",
				"https://fpcy.ha-n-tax.gov.cn"));
		citys.add(new InvoiceCityEntity("4200", "湖北",
				"https://fpcy.hb-n-tax.gov.cn:443",
				"https://fpcy.hb-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("4300", "湖南",
				"https://fpcy.hntax.gov.cn:8083",
				"https://fpcy.hntax.gov.cn:8083"));
		citys.add(new InvoiceCityEntity("4400", "广东",
				"https://fpcy.gd-n-tax.gov.cn:443",
				"https://fpcy.gd-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("4403", "深圳",
				"https://fpcy.szgs.gov.cn:443",
				"https://fpcy.szgs.gov.cn:443"));
		citys.add(new InvoiceCityEntity("4500", "广西",
				"https://fpcy.gxgs.gov.cn:8200",
				"https://fpcy.gxgs.gov.cn:8200"));
		citys.add(new InvoiceCityEntity("4600", "海南",
				"https://fpcy.hitax.gov.cn:443",
				"https://fpcy.hitax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("5000", "重庆",
				"https://fpcy.cqsw.gov.cn:80", "https://fpcy.cqsw.gov.cn:80"));
		citys.add(new InvoiceCityEntity("5100", "四川",
				"https://fpcy.sc-n-tax.gov.cn:443",
				"https://fpcy.sc-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("5200", "贵州",
				"https://fpcy.gz-n-tax.gov.cn:80",
				"https://fpcy.gz-n-tax.gov.cn:80"));
		citys.add(new InvoiceCityEntity("5300", "云南",
				"https://fpcy.yngs.gov.cn:443",
				"https://fpcy.yngs.gov.cn:443"));
		citys.add(new InvoiceCityEntity("5400", "西藏",
				"https://fpcy.xztax.gov.cn:81",
				"https://fpcy.xztax.gov.cn:81"));
		citys.add(new InvoiceCityEntity("6100", "陕西",
				"https://fpcyweb.sn-n-tax.gov.cn:443",
				"https://fpcyweb.sn-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("6200", "甘肃",
				"https://fpcy.gs-n-tax.gov.cn:443",
				"https://fpcy.gs-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("6300", "青海",
				"https://fpcy.qh-n-tax.gov.cn:443",
				"https://fpcy.qh-n-tax.gov.cn:443"));
		citys.add(new InvoiceCityEntity("6400", "宁夏",
				"https://fpcy.nxgs.gov.cn:443",
				"https://fpcy.nxgs.gov.cn:443"));
		citys.add(new InvoiceCityEntity("6500", "新疆",
				"https://fpcy.xj-n-tax.gov.cn:443",
				"https://fpcy.xj-n-tax.gov.cn:443"));

		codes.add("144031539110");
		codes.add("131001570151");
		codes.add("133011501118");
		codes.add("111001571071");


		comments.put("fpcc", "发票名称");
		comments.put("fpdm", "发票代码");
		comments.put("fphm", "发票号码");
		comments.put("kprq", "开票日期");
		comments.put("jym", "校验码");
		comments.put("jqbh", "机器编号");
		comments.put("gfmc", "购买方名称");
		comments.put("gfsbh", "购买方纳税人识别号");
		comments.put("gfdzdh", "购买方地址电话");
		comments.put("gfyhzh", "购买方银行帐号");
		comments.put("xfmc", "销售方名称");
		comments.put("xfsbh", "购买方纳税人识别号");
		comments.put("xfyhzh", "销售方银行帐号");
		comments.put("xfdzdh", "购买方地址电话");
		comments.put("je", "不含税金额");
		comments.put("se", "税额");
		comments.put("jshjdx", "价税合计(大写)");
		comments.put("jshjxx", "价税合计(小写)");

		
		comments.put("xmmc","费用项目名称");
		comments.put("xh","规格型号");
		comments.put("dw","单位");
		comments.put("sl","数量");
		comments.put("dj","单价");
		comments.put("rate","税率");
		comments.put("se","税额");


	}

	/**
	 * 根据发票代码来获取请求的URL前缀
	 * 
	 * @param fpdm
	 *            发票代码
	 * @return 请求URL
	 */
	public static String getPrefixURL(String fpdm) {
		// 地区代码
		String dqdm = "";
		String prefixURL = "";

		if (fpdm.length() == 12) {
			dqdm = fpdm.substring(1, 5);
		} else {
			dqdm = fpdm.substring(0, 4);
		}

		if (!dqdm.equals("2102") && !dqdm.equals("3302") && !dqdm.equals("3502") && !dqdm.equals("3702")
				&& !dqdm.equals("4403")) {
			dqdm = dqdm.substring(0, 2) + "00";
		}

		for (InvoiceCityEntity entity : citys) {
			if (entity.getCode().equals(dqdm)) {
				prefixURL = entity.getIp();
				break;
			}
		}
		return prefixURL;
	}
	
	/**
	 * 根据发票代码来获取发票省份
	 * 
	 * @param fpdm
	 *            发票代码
	 * @return 发票省份
	 */
	public static String getProvince(String fpdm) {
		// 地区代码
		String dqdm = "";
		String province = "";

		if (fpdm.length() == 12) {
			dqdm = fpdm.substring(1, 5);
		} else {
			dqdm = fpdm.substring(0, 4);
		}

		if (!dqdm.equals("2102") && !dqdm.equals("3302") && !dqdm.equals("3502") && !dqdm.equals("3702")
				&& !dqdm.equals("4403")) {
			dqdm = dqdm.substring(0, 2) + "00";
		}

		for (InvoiceCityEntity entity : citys) {
			if (entity.getCode().equals(dqdm)) {
				province = entity.getProvince();
				break;
			}
		}
		return province;
	}
	
	/**
	 * 获取验证码请求URL
	 * @param fpdm 发票代码
	 * @return
	 */
	public static String getYzmUrl(String fpdm) {
		return getPrefixURL(fpdm) + URL_SUBFIX_YZM;
	}
	
	/**
	 * 获取完整数据请求URL
	 * @param fpdm 发票代码
	 * @return
	 */
	public static String getDetailUrl(String fpdm) {
		return getPrefixURL(fpdm) + URL_SUBFIX_DETAIL;
	}

	/**
	 * 根据发票代码获取发票类型 </br>
	 * 参考:</br>
	 * <strong>10</strong>--增值税电子普通发票：(12位)第一位为0，最后两位为11 </br>
	 * <strong>04</strong>--增值税普通发票: (12位) 第一位为0，最后两位为04或者05 (10位)第八位为6或者3 </br>
	 * <strong>11</strong>--增值税普通发票(卷票):(12位)第一位为0，最后两位为06或者07 </br>
	 * <strong>14</strong>--增值税普通发票(通行费):(12位)第一位为0，最后两位为12 </br>
	 * <strong>03</strong>--机动车销售统一发票(12位)除上以外，第一位为0，第8位为2 </br>
	 * <strong>01</strong>--增值税专用发票:(10位) 第八位为1或者5 </br>
	 * <strong>02</strong>--货物运输业增值税专用发票:(10位) 第八位为7或者2 </br>
	 * @param fpdm  发票代码     
	 * @return 发票类型
	 */
	public static String getInvoiceType(String fpdm) {

		String invoiceType = "99";
		String tmp = "";
		if (fpdm.length() == 12) {
			tmp = fpdm.substring(7, 8);

			for (String s : codes) {
				if (s.equals(fpdm)) {
					invoiceType = "10";
					break;
				}
			}

			if (invoiceType == "99") {
				if (fpdm.charAt(0) == '0' && fpdm.substring(10, 12).equals("11")) {
					invoiceType = "10";
				}
				if (fpdm.charAt(0) == '0' && (fpdm.substring(10, 12).equals("04")
						|| fpdm.substring(10, 12).equals("05"))) {
					invoiceType = "04";
				}
				if (fpdm.charAt(0) == '0' && (fpdm.substring(10, 12).equals("06")
						|| fpdm.substring(10, 12).equals("07"))) {
					invoiceType = "11";
				}
				if (fpdm.charAt(0) == '0' && fpdm.substring(10, 12).equals("12")) {
					invoiceType = "14";
				}
			}

			if (invoiceType == "99") {
				if (invoiceType.equals("99") && tmp.equals("2")
						&& fpdm.charAt(0) != '0') {
					invoiceType = "03";
				}
			}
		} else if (fpdm.length() == 10) {
			tmp = fpdm.substring(7, 8);

			if (tmp.equals("1") || tmp.equals("5")) {
				invoiceType = "01";
			} else if (tmp.equals("6") || tmp.equals("3")) {
				invoiceType = "04";
			} else if (tmp.equals("7") || tmp.equals("2")) {
				invoiceType = "02";
			}
		}

		return invoiceType;
	}

	/**
	 * 获取发票上字段相应的名称解释
	 * @param name
	 * @return
	 */
	public static String getComment(String name){
		return comments.get(name);
	}
	
	
	
	public static ResultEnum getResultWithCode(String code) {
		ResultEnum re;
		
		if ("1".equals(code)) {re = ResultEnum.INVOICE_VERIFY_NOT_OPEN;}
		else if ("001".equals(code)) {re = ResultEnum.SUCCESS;}
		else if ("002".equals(code)) {re = ResultEnum.INVOICE_VERIFY_NO_COUNT;}
		else if ("003".equals(code)) {re = ResultEnum.INVOICE_VERIFY_SEQUENCE;}
		else if ("004".equals(code)) {re = ResultEnum.INVOICE_VERIFY_MAX_CONNECT;}
		else if ("005".equals(code)) {re = ResultEnum.INVOICE_VERIFY_NOT_ILLEGAL;}
		else if ("006".equals(code)) {re = ResultEnum.INVOICE_VERIFY_NOT_CORRECT;}
		else if ("007".equals(code)) {re = ResultEnum.INVOICE_VERIFY_NOT_YZM_AVALIABLE;}
		else if ("008".equals(code)) {re = ResultEnum.INVOICE_VERIFY_NOT_YZM_CORRECT;}
		else if ("009".equals(code)) {re = ResultEnum.INVOICE_VERIFY_NOT_EXIST;}
		else if ("020".equals(code)) {re = ResultEnum.INVOICE_VERIFY_NOT_ALLOW;}	
		else  {re = ResultEnum.INVOICE_VERIFY_NOT_CONNECT;}
		
		return re;
	}

}
class InvoiceCityEntity {

	private String code;
	private String province;
	private String ip;
	private String address;

	public InvoiceCityEntity(String code, String province, String ip,
			String address) {
		this.code = code;
		this.province = province;
		this.ip = ip;
		this.address = address;

	}

	public String getCode() {
		return code;
	}

	public String getProvince() {
		return province;
	}

	public String getIp() {
		return ip;
	}

	public String getAddress() {
		return address;
	}

}
